#ifndef MIDI_H
#define MIDI_H

#include <alsa/asoundlib.h>

class Midi  {
public:
     // echo data types used for internal control
     enum {
	  None,
	  Loop,
	  IncRow
     } EchoType;
     static const int ppq = 128;

     Midi();
     ~Midi();
     void createPorts();
     void createQueue();
     void sendEvent(snd_seq_event_t *ev);
     void setTempo(int bpm);
     void scheduleNoteOn(int chan, int note, int vel, int tick);
     void scheduleNoteOff(int chan, int oldNote, int tick);
     void scheduleControlChange(int chan, int num, int val, int tick);
     void scheduleEcho(int tick, int data=Loop);
     void playNote(int num, int vel, int len, int chan);
     void abort();
     int checkInputEvents();
     // default arg - clear all queue events
     // if tick is set, clear events after given tick
     void clearQueue(int afterTick=0);
     void allNotesOff();

private:
     static const int PollInterval = 1000;

     snd_seq_t *m_pSeq;
     int m_outPortId;
     int m_inPortId;
     int m_queueId;
     int m_pollFdCount;
     struct pollfd *m_pPollFd;
     bool m_abort;

     int handleInputEvents();
     void initPollDescriptors();
};

#endif
